package com.ruoyi.user.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.check.CheckEmptyUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.user.domain.UserCoupon;
import com.ruoyi.user.mapper.UserCouponMapper;
import com.ruoyi.user.mapper.UserMapper;
import com.ruoyi.user.service.IUserCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 持有优惠卷Controller
 *
 * @author ruoyi
 * @date 2025-04-10
 */
@RestController
@RequestMapping("/userCoupon/userCoupon")
public class UserCouponController extends BaseController {
    @Autowired
    private IUserCouponService userCouponService;
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserCouponMapper userCouponMapper;

    /**
     * 查询持有优惠卷列表
     */
    @PreAuthorize("@ss.hasPermi('userCoupon:userCoupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserCoupon userCoupon) {
        startPage();
        List<UserCoupon> list = userCouponService.selectUserCouponList(userCoupon);
        if (!CheckEmptyUtil.isEmpty(list)) {
            for (UserCoupon config : list) {
                if (config.getCoupomScopeApplication() != 0) {
                    String tName = userMapper.selectTName(Long.valueOf(config.getCoupomScopeApplication()));
                    config.setTechnicianName(CheckEmptyUtil.isEmpty(tName) ? "" : tName);
                }
                String couponName = userCouponMapper.selectCouponName(config.getCouponId());
                config.setCouponName(CheckEmptyUtil.isEmpty(couponName) ? "" : couponName);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出持有优惠卷列表
     */
    @PreAuthorize("@ss.hasPermi('userCoupon:userCoupon:export')")
    @Log(title = "持有优惠卷", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserCoupon userCoupon) {
        List<UserCoupon> list = userCouponService.selectUserCouponList(userCoupon);
        if (!CheckEmptyUtil.isEmpty(list)) {
            for (UserCoupon config : list) {
                if (config.getCoupomScopeApplication() != 0) {
                    String tName = userMapper.selectTName(Long.valueOf(config.getCoupomScopeApplication()));
                    config.setTechnicianName(CheckEmptyUtil.isEmpty(tName) ? "" : tName);
                }
                String couponName = userCouponMapper.selectCouponName(config.getCouponId());
                config.setCouponName(CheckEmptyUtil.isEmpty(couponName) ? "" : couponName);
            }
        }
        ExcelUtil<UserCoupon> util = new ExcelUtil<UserCoupon>(UserCoupon.class);
        util.exportExcel(response, list, "持有优惠卷数据");
    }

    /**
     * 获取持有优惠卷详细信息
     */
    @PreAuthorize("@ss.hasPermi('userCoupon:userCoupon:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        UserCoupon userCoupon = userCouponService.selectUserCouponById(id);
        if (!CheckEmptyUtil.isEmpty(userCoupon)) {
            if (userCoupon.getCoupomScopeApplication() != 0) {
                String tName = userMapper.selectTName(Long.valueOf(userCoupon.getCoupomScopeApplication()));
                userCoupon.setTechnicianName(CheckEmptyUtil.isEmpty(tName) ? "" : tName);
            }
                String couponName = userCouponMapper.selectCouponName(userCoupon.getCouponId());
                userCoupon.setCouponName(CheckEmptyUtil.isEmpty(couponName) ? "" : couponName);

        }
        return success(userCoupon);
    }

    /**
     * 新增持有优惠卷
     */
    @PreAuthorize("@ss.hasPermi('userCoupon:userCoupon:add')")
    @Log(title = "持有优惠卷", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserCoupon userCoupon) {

        return toAjax(userCouponService.insertUserCoupon(userCoupon));
    }

    /**
     * 修改持有优惠卷
     */
    @PreAuthorize("@ss.hasPermi('userCoupon:userCoupon:edit')")
    @Log(title = "持有优惠卷", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserCoupon userCoupon) {
        return toAjax(userCouponService.updateUserCoupon(userCoupon));
    }

    /**
     * 删除持有优惠卷
     */
    @PreAuthorize("@ss.hasPermi('userCoupon:userCoupon:remove')")
    @Log(title = "持有优惠卷", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userCouponService.deleteUserCouponByIds(ids));
    }
}
